package com.Lacia.emsserver.utils;

import com.Lacia.emsserver.common.BmsDataAccumulator;
import com.Lacia.emsserver.pojo.*;

import java.time.Instant;
import java.util.Random;

/**
 * 生成模拟数据方法
 * @author 万里花
 * @date 2024/6/5 15:59
 */

public class GenData {


    private static final Random random = new Random();


    /**
     * 生成pcs数据
     * @return
     */
    public static PcsPushData generateRandomEmsData() {
        PcsPushData emsData = new PcsPushData();

        // Assign random values
        // 电网UV电压
        emsData.setGrdUvVol(randomDouble(200.0, 250.0, 0.1));
        // 电网VW电压
        emsData.setGrdVwVol(randomDouble(200.0, 250.0, 0.1));
        // 电网WU电压
        emsData.setGrdWuVol(randomDouble(200.0, 250.0, 0.1));
        // 环境温度
        emsData.setEnvTemp(randomDouble(-10.0, 40.0, 0.1));
        emsData.setTimestamp(Instant.now());
        emsData.setArrNum(0);
        emsData.setCluNum(0);
        emsData.setDevNum(0);
        // 电网U电压
        emsData.setGrdUVol(randomDouble(200.0, 250.0, 0.1));
        // 电网V电压
        emsData.setGrdVVol(randomDouble(200.0, 250.0, 0.1));
        // 电网W电压
        emsData.setGrdWVol(randomDouble(200.0, 250.0, 0.1));
        // 输出电流
        emsData.setOutUCur(randomDouble(0.0, 100.0, 0.1));
        // 输出电压
        emsData.setOutVCur(randomDouble(0.0, 100.0, 0.1));
        // 输出电流
        emsData.setOutWCur(randomDouble(0.0, 100.0, 0.1));
        // 电网频率
        emsData.setGrdFreq(randomDouble(49.0, 51.0, 0.01));
        emsData.setAcUPowP(randomDouble(0.0, 500.0, 0.1));
        emsData.setAcVPowP(randomDouble(0.0, 500.0, 0.1));
        emsData.setAcWPowP(randomDouble(0.0, 500.0, 0.1));
        emsData.setAcUPowS(randomDouble(0.0, 500.0, 0.1));
        emsData.setAcVPowS(randomDouble(0.0, 500.0, 0.1));
        emsData.setAcWPowS(randomDouble(0.0, 500.0, 0.1));
        emsData.setAcUPowQ(randomDouble(0.0, 500.0, 0.1));
        emsData.setAcVPowQ(randomDouble(0.0, 500.0, 0.1));
        emsData.setAcWPowQ(randomDouble(0.0, 500.0, 0.1));
        emsData.setAcPowP(randomDouble(0.0, 1500.0, 0.1));
        emsData.setAcPowQ(randomDouble(0.0, 1500.0, 0.1));
        emsData.setAcPowS(randomDouble(0.0, 1500.0, 0.1));
        emsData.setAcPowPf(randomDouble(0.0, 1.0, 0.01));
        emsData.setBusVoltage(randomDouble(500.0, 700.0, 0.1));
        emsData.setBatteryVoltage(randomDouble(48.0, 56.0, 0.1));
        emsData.setBatteryCurrent(randomDouble(0.0, 200.0, 0.1));
        emsData.setDcPow(randomDouble(0.0, 1000.0, 0.1));
        emsData.setIgbtTemp(randomDouble(0.0, 100.0, 0.1));
        emsData.setModuleTmp(randomDouble(0.0, 100.0, 0.1));
        // 0:直流电压源模式，1:交流电流源模式
        emsData.setRunModSta(randomInt(0, 1));
        emsData.setAcUPowPf(randomDouble(0.0, 1.0, 0.01));
        emsData.setAcVPowPf(randomDouble(0.0, 1.0, 0.01));
        emsData.setAcWPowPf(randomDouble(0.0, 1.0, 0.01));
        emsData.setChgCap(randomInt(0, 1000));
        emsData.setChgdayCap(randomInt(0, 100));
        emsData.setDisCap(randomInt(0, 1000));
        emsData.setDisdayCap(randomInt(0, 100));
        emsData.setComchgCap(randomInt(0, 1000));
        emsData.setComdisCap(randomInt(0, 1000));
        emsData.setComdisdayCap(randomInt(0, 100));
        // 0:停机，1:待机，2:故障，3:充电，4:放电，5:充电降功率，6:放电降功率
        emsData.setRunStatus(randomInt(0, 6));
        emsData.setNegBusVoltage(randomDouble(200.0, 250.0, 0.1));
        emsData.setPosBusVoltage(randomDouble(200.0, 250.0, 0.1));
        emsData.setComchgdayCap(randomInt(0, 100));

        return emsData;
    }


    /**
     * 生成电池堆数据
     * @return
     */
    public static BmsStackData generateRandomBmsStackData() {
        BmsStackData bmsData = new BmsStackData();

        // Assign random values
        // 0:停机，1:充电，2:放电，3:待机，4:故障
        bmsData.setArrStatus(randomInt(0, 4));
        bmsData.setArrVol(randomDouble(0.0, 1000.0, 0.1));
        bmsData.setArrCur(randomDouble(0.0, 1000.0, 0.1));
        bmsData.setArrSOC(randomInt(0, 100));
        bmsData.setArrSOH(randomInt(0, 100));
        bmsData.setArrRechaElec(randomDouble(0.0, 1000.0, 0.1));
        bmsData.setArrRedischgElec(randomDouble(0.0, 1000.0, 0.1));

        // 电池堆充电量累加值与电池堆放电量累加值
        BmsDataAccumulator.addToChargeTotal(4);
        BmsDataAccumulator.addToDischargeTotal(5);
        bmsData.setArrChaElecTotal(BmsDataAccumulator.getChargeTotal());
        bmsData.setArrDischgElecTotal(BmsDataAccumulator.getDischargeTotal());

        // 确保最高单体电压不低于最低单体电压
        double cellMaxVol = randomDouble(0.0, 5.0, 0.001);
        double cellMinVol = randomDouble(0.0, 5.0, 0.001);
        if (cellMaxVol < cellMinVol) {
            double temp = cellMaxVol;
            cellMaxVol = cellMinVol;
            cellMinVol = temp;
        }
        bmsData.setCellMaxVol(cellMaxVol);
        bmsData.setCellMinVol(cellMinVol);

        // 确保最高温度不低于最低温度
        int cellMaxTem = randomInt(-40, 125);
        int cellMinTem = randomInt(-40, 125);
        if (cellMaxTem < cellMinTem) {
            int temp = cellMaxTem;
            cellMaxTem = cellMinTem;
            cellMinTem = temp;
        }
        bmsData.setCellMaxTem(cellMaxTem);
        bmsData.setCellMinTem(cellMinTem);

        bmsData.setTimestamp(Instant.now());
        bmsData.setArrLastRechaElec(randomDouble(0.0, 1000.0, 0.1));
        bmsData.setArrLastRedischgElec(randomDouble(0.0, 1000.0, 0.1));
        bmsData.setArrMaxReChaPower(randomDouble(0.0, 1000.0, 0.1));
        bmsData.setArrMaxReDischgPower(randomDouble(0.0, 1000.0, 0.1));
        bmsData.setArrMaxReChaCur(randomDouble(0.0, 1000.0, 0.1));
        bmsData.setArrMaxRedischgCur(randomDouble(0.0, 1000.0, 0.1));
        // 0:正常，1:预警，2:告警，3:保护
        bmsData.setBmsStatus(randomInt(0, 3));
        // 堆号固定0
        bmsData.setArrNum(0);
        bmsData.setCluMaxSoc(randomInt(0, 100));
        bmsData.setCluMinSoc(randomInt(0, 100));
        bmsData.setCluMaxVol(randomDouble(0.0, 1000.0, 0.1));
        bmsData.setCluMinVol(randomDouble(0.0, 1000.0, 0.1));
        bmsData.setCellAveVol(randomDouble(0.0, 5.0, 0.001));
        bmsData.setCellAveTem(randomInt(-40, 125));
        bmsData.setCluSums(randomInt(0, 100));
        bmsData.setCluMaxSocNum(randomInt(0, 100));
        bmsData.setCluMinSocNum(randomInt(0, 100));
        bmsData.setCluMaxVolNum(randomInt(0, 100));
        bmsData.setCluMinVolNum(randomInt(0, 100));
        bmsData.setCellMaxVolCluNum(randomInt(0, 100));
        bmsData.setCellMaxVolNum(randomInt(0, 100));
        bmsData.setCellMinVolCluNum(randomInt(0, 100));
        bmsData.setCellMinVolNum(randomInt(0, 100));
        bmsData.setCellMaxTemCluNum(randomInt(0, 100));
        bmsData.setCellMaxTemNum(randomInt(0, 100));
        bmsData.setCellMinTemCluNum(randomInt(0, 100));
        bmsData.setCellMinTemNum(randomInt(0, 100));
        return bmsData;
    }


    /**
     * 生成电池簇数据
     * @return
     */
    public static BmsClusterMyData generateRandomBmsClusterMyData() {
        BmsClusterMyData bmsData = new BmsClusterMyData();
        bmsData.setTimestamp(Instant.now());
        bmsData.setCluSoc(randomDouble(0, 100, 1));
        bmsData.setCluSoh(randomDouble(0, 100, 1));
        bmsData.setCluVol(randomDouble(0, 1000, 0.1));
        bmsData.setCluCur(randomDouble(0, 1000, 0.1));
        bmsData.setCellMaxVol(randomDouble(0, 5, 0.001));
        bmsData.setCellMinVol(randomDouble(0, 5, 0.001));
        bmsData.setCellMaxTem(randomInt(-40, 125));
        bmsData.setCellMinTem(randomInt(-40, 125));
        bmsData.setCluRtPower(randomDouble(0, 1000, 0.1));
        bmsData.setCluRunStatus(randomInt(0, 4));
        bmsData.setCluChaElecTotal(randomDouble(0, 10000, 0.1));
        bmsData.setCluDischgElecTotal(randomDouble(0, 10000, 0.1));
        bmsData.setLastChaElec(randomDouble(0, 1000, 0.1));
        bmsData.setLastDischgElec(randomDouble(0, 1000, 0.1));
        bmsData.setCluRechaElec(randomDouble(0, 1000, 0.1));
        bmsData.setCluRedischgElec(randomDouble(0, 1000, 0.1));
        // 堆号固定为0
        bmsData.setArrNum(0);
        // 簇号固定为0
        bmsData.setCluNum(0);
        bmsData.setBmsStatus(randomInt(0, 3));
        bmsData.setCluCellAveVol(randomDouble(0, 5, 0.001));
        bmsData.setCellMaxSoc(randomDouble(0, 100, 1));
        bmsData.setCellMinSoc(randomDouble(0, 100, 1));
        bmsData.setCellMaxSoh(randomDouble(0, 100, 1));
        bmsData.setCellMinSoh(randomDouble(0, 100, 1));
        bmsData.setCluPosres(randomDouble(0, 1000, 1));
        bmsData.setCluNegres(randomDouble(0, 1000, 1));
        bmsData.setCellMaxVolNum(randomInt(0, 100));
        bmsData.setCellMinVolNum(randomInt(0, 100));
        bmsData.setCellMaxTemNum(randomInt(0, 100));
        bmsData.setCellMinTemNum(randomInt(0, 100));
        bmsData.setCellVolTotal(randomDouble(0, 1000, 0.1));
        bmsData.setMaxDischgArrCur(randomDouble(0, 1000, 0.1));
        return bmsData;
    }


    /**
     * 生成电池簇单体数据
     * @return
     */
    public static BmsClusterMonomerData generateRandomBmsClusterMonomerData() {
        BmsClusterMonomerData data = new BmsClusterMonomerData();
        // 堆号固定为0
        data.setArrNum(0);
        // 簇号固定为0
        data.setCluNum(0);
        data.setTimestamp(Instant.now());
        data.setCelVol(randomDouble(0, 5, 0.01));
        data.setCelTem(randomDouble(-40, 125, 0.1));
        data.setCelSoc(randomDouble(0, 100, 1));
        data.setCelSoh(randomDouble(0, 100, 1));

        return data;
    }



    /**
     * 计量电表
     * 生成随机的 MeterMyData 对象
     * @return 包含随机数据的 MeterMyData 对象
     */
    public static MeterMyData generateRandomMeterMyData() {
        MeterMyData data = new MeterMyData();
        // 总有功功率
        data.setMeterTotP(randomDouble(0, 100, 0.001));
        // 当前正向总尖有功电能
        data.setMeterVerPos(randomDouble(0, 1000, 0.01));
        // 当前正向总峰有功电能
        data.setMeterHigPos(randomDouble(0, 1000, 0.01));
        // 当前正向总平有功电能
        data.setMeterMidPos(randomDouble(0, 1000, 0.01));
        // 当前正向总谷有功电能
        data.setMeterLowPos(randomDouble(0, 1000, 0.01));
        // 当前反向总有功电能
        data.setMeterAddNeg(randomDouble(0, 1000, 0.01));
        // 当前反向总尖有功电能
        data.setMeterVerNeg(randomDouble(0, 1000, 0.01));
        // 当前反向总峰有功电能
        data.setMeterHigNeg(randomDouble(0, 1000, 0.01));
        // 当前反向总平有功电能
        data.setMeterMidNeg(randomDouble(0, 1000, 0.01));
        // 当前反向总谷有功电能
        data.setMeterLowNeg(randomDouble(0, 1000, 0.01));
        // 时间戳
        data.setTimestamp(Instant.now());
        // 总无功功率
        data.setMeterTotQ(randomDouble(0, 100, 0.001));
        // 当前组合有功尖电能
        data.setMeterVerP(randomDouble(0, 1000, 0.01));
        // 当前组合有功峰电能
        data.setMeterHigP(randomDouble(0, 1000, 0.01));
        // 当前组合有功平电能
        data.setMeterMidP(randomDouble(0, 1000, 0.01));
        // 当前组合有功谷电能
        data.setMeterLowP(randomDouble(0, 1000, 0.01));
        // 当前组合无功尖电能
        data.setMeterVerQ(randomDouble(0, 1000, 0.01));
        // 当前组合无功峰电能
        data.setMeterHigQ(randomDouble(0, 1000, 0.01));
        // 当前组合无功平电能
        data.setMeterMidQ(randomDouble(0, 1000, 0.01));
        // 当前组合无功谷电能
        data.setMeterLowQ(randomDouble(0, 1000, 0.01));
        // A相正向有功电能
        data.setMeterPosA(randomDouble(0, 1000, 0.01));
        // B相正向有功电能
        data.setMeterPosB(randomDouble(0, 1000, 0.01));
        // C相正向有功电能
        data.setMeterPosC(randomDouble(0, 1000, 0.01));
        // 堆号(户外柜默认0)
        data.setArrNum(0);
        // 簇号(户外柜默认0)
        data.setCluNum(0);
        // 设备号(户外柜默认0)
        data.setDevNum(0);
        // 频率
        data.setMeterFreq(randomDouble(49, 51, 0.01));
        // a相电压
        data.setMeterVolA(randomDouble(220, 240, 0.1));
        // b相电压
        data.setMeterVolB(randomDouble(220, 240, 0.1));
        // c相电压
        data.setMeterVolC(randomDouble(220, 240, 0.1));
        // a相电流
        data.setMeterCurA(randomDouble(0, 100, 0.01));
        // b相电流
        data.setMeterCurB(randomDouble(0, 100, 0.01));
        // c相电流
        data.setMeterCurC(randomDouble(0, 100, 0.01));
        // 总功率因数
        data.setMeterTotPf(randomDouble(0, 1, 0.001));
        // 当前组合有功总电能
        data.setMeterAddP(randomDouble(0, 1000, 0.01));
        // 当前组合无功总电能
        data.setMeterAddQ(randomDouble(0, 1000, 0.01));
        // 当前正向总有功电能
        data.setMeterAddPos(randomDouble(0, 1000, 0.01));
        return data;
    }


    /**
     * 液冷数据
     * 生成随机的 LiqcoolData 对象
     * @return
     */
    public static LiqcoolData generateRandomLiqcoolData() {
        LiqcoolData data = new LiqcoolData();

        // 堆号(户外柜默认0)
        data.setArrNum(0);
        // 簇号(户外柜默认0)
        data.setCluNum(-1);
        // 设备号(户外柜默认0)
        data.setDevNum(0);
        // 时间戳
        data.setTimestamp(Instant.now().plusMillis(random.nextInt(1000)));
        // 环境温度
        data.setLiqOuttmp(randomDouble(0, 100, 0.1));
        // 系统出温
        data.setLiqhotWaterTmp(randomDouble(0, 100, 0.1));
        // 系统回温
        data.setSjBackwatertmp(randomInt(0, 100));
        // 出水压力
        data.setOutWaterpre(randomInt(0, 1000));
        // 回水压力
        data.setBackWaterpr(randomInt(0, 1000));
        // 循环泵1运行状态
        data.setLiqPump(random.nextInt(2));
        // PTC1运行状态
        data.setLiqColdsta(random.nextInt(3));
        // 补水泵运行状态
        data.setRepumpSta(random.nextInt(3));
        // 1#压机运行时间
        data.setCompRuntime1(randomInt(0, 10000));
        // 1#设定转速
        data.setCompresSpeed(randomInt(0, 10000));
        // 1#当前转速
        data.setLiqpumpSpeed(randomInt(0, 10000));
        // 1#变频器输出电压
        data.setOutVoltage(randomInt(0, 1000));
        // 1#吸气温度
        data.setInspiratoryTmp1(randomDouble(0, 100, 0.1));
        // 1#排气温度
        data.setOutspiratoryTmp1(randomDouble(0, 100, 0.1));
        // 1#高压压力
        data.setHighPre1(randomInt(0, 1000));
        // 1#低压压力
        data.setLowPre1(randomInt(0, 1000));
        // 1#吸气饱和温度
        data.setInsSatTem1(randomInt(0, 100));
        // 1#目标过热度
        data.setSysHotDegree1(randomDouble(0, 100, 0.1));
        // 2#压机运行时间
        data.setCompRuntime2(randomInt(0, 10000));
        // 2#吸气温度
        data.setInspiratoryTem2(randomInt(0, 100));
        // 2#排气温度
        data.setOutspiratoryTem2(randomInt(0, 100));
        // 2#高压压力
        data.setHighPre2(randomInt(0, 1000));
        // 2#低压压力
        data.setLowPre2(randomInt(0, 1000));

        return data;
    }










    private static Double randomDouble(double min, double max, double precision) {
        double scale = 1.0 / precision;
        return Math.round((min + (max - min) * random.nextDouble()) * scale) / scale;
    }

    private static Integer randomInt(int min, int max) {
        return random.nextInt(max - min + 1) + min;
    }


}
